package com.example.administrator.yunniuusedcar;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.administrator.yunniuusedcar.adapter.MainAdapter;
import com.example.administrator.common.base.BaseActivity;
import com.example.administrator.common.base.Global;
import com.example.administrator.common.ui.TabBar;
import com.example.administrator.yunniuusedcar.fragment.MainChooseCarFragment3Model;
import com.example.administrator.yunniuusedcar.fragment.MainHomeFragment1Model;
import com.example.administrator.yunniuusedcar.fragment.MainInformationFragment2Model;
import com.example.administrator.yunniuusedcar.fragment.MineFragment5Model;
import com.example.administrator.yunniuusedcar.fragment.MainToolsFragment4Model;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {   //AppCompatActivity

    private String[] mTitles = new String[]{
            "首页", "资讯", "选车", "工具", "我的"
    };

    private int[] mTabIcons = new int[]{
            R.mipmap.icon_tab_01,  //首页
            R.mipmap.icon_tab_02,    //资讯
            R.mipmap.icon_tab_03,    //选车
            R.mipmap.truetool,        // 工具
            R.mipmap.icon_tab_04,     //我的
    };

    private int[] mTabIconsSelected = new int[]{
            R.mipmap.icon_tab_01_selected,
            R.mipmap.icon_tab_02_selected,
            R.mipmap.icon_tab_03_selected,
            R.mipmap.truestool,
            R.mipmap.icon_tab_04_selected
    };
    private TabBar tabBar;
    private ViewPager viewPager;
    private LinearLayout mLlTabLayout;
    private TextView tv_Address;
    private ImageView iv_Search;
    private ImageView iv_Shard;
    public static MainActivity mactivity;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {   /////////////
        super.onCreate(savedInstanceState);
        mactivity = this;    //全局 传can
                  initView();
    }

    @Override
    public int getLayoutRes() {
        return R.layout.activity_test;
    }
    @Override
    public void initView() {

        initViewPager();
        initTabBar();

        // 判断是否有虚拟按键（导航栏）
        // if (有虚拟按键 && sdkVersion >= 4.4) {
        if (Global.hasNavigationBar(this) &&
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int navigationHeight = Global.getNavigationBarHeight(this);
            ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
            root.setPadding(root.getLeft(), root.getTop(),
                    root.getRight(), navigationHeight);
        }
        mLlTabLayout = (LinearLayout) findViewById(R.id.ll_tab_layout);
    }

    // 显示ViewPager
    private void initViewPager() {
        viewPager = findView(R.id.view_pager);

        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new MainHomeFragment1Model());                 // 首页
        fragments.add(new MainInformationFragment2Model());          // 资讯
        fragments.add(new MainChooseCarFragment3Model());            // 选车
        fragments.add(new MainToolsFragment4Model());                // 工具
        fragments.add(new MineFragment5Model());                 // 我的

        MainAdapter mainAdapter = new MainAdapter(
                getSupportFragmentManager(), fragments);
        viewPager.setAdapter(mainAdapter);
    }

    // 初始化选项卡
    private void initTabBar() {
        tabBar = findView(R.id.tab_bar);
        tabBar.setTabColorNormal(Color.GRAY);        // 选项卡默认状态的颜色
        tabBar.setTabColorSelected(Color.RED);       // 选项卡选中状态的颜色
        tabBar.setIconSize(Global.dp2px(20), Global.dp2px(20));  // 选项卡图标的宽高
        tabBar.setPaddingBetweenTextAndIcon(Global.dp2px(5));    // 选项卡中图标和文字的间距

        // tabBar.setTabDatas(mTitles, mTabIcons);
        tabBar.setTabDatas(mTitles, mTabIcons, mTabIconsSelected);

        // 选项卡切换监听
        tabBar.setOnTabSelectedListener(new TabBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                viewPager.setCurrentItem(position, true);   // 单选 可 滑动()
            }
        });
    }
    @Override
    public void initListener() {
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override   // 子界面切换时，调用此方法
            public void onPageSelected(int position) {
                // 高亮并选中对应的选项卡
                tabBar.setTabSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    @Override
    public void initData() {

    }

}




